{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'))\n",
    "print(df.head())\n"
   ],
   "metadata": {
    "collapsed": false,
    "is_executing": true
   },
   "id": "2452fb721ea11b6e"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# query "
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f2dc7673362ca7af"
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "outputs": [
    {
     "data": {
      "text/plain": "          A         B         C         D\n0 -0.468850 -0.575355 -1.755448 -0.721407\n1  0.626830  0.142729  0.959875  0.695038\n2 -0.325520 -1.051279  0.077001 -0.976853\n3  0.620873  0.115614  0.203067 -1.259401\n5  0.424593  0.006147 -0.542266  0.060362\n6  0.326262 -0.133568 -0.246196 -1.293947\n8  0.508724  0.491179  0.511179 -0.430321\n9 -0.132347 -0.218552 -0.705951  1.288782",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>-0.468850</td>\n      <td>-0.575355</td>\n      <td>-1.755448</td>\n      <td>-0.721407</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.626830</td>\n      <td>0.142729</td>\n      <td>0.959875</td>\n      <td>0.695038</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>-0.325520</td>\n      <td>-1.051279</td>\n      <td>0.077001</td>\n      <td>-0.976853</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0.620873</td>\n      <td>0.115614</td>\n      <td>0.203067</td>\n      <td>-1.259401</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>0.424593</td>\n      <td>0.006147</td>\n      <td>-0.542266</td>\n      <td>0.060362</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>0.326262</td>\n      <td>-0.133568</td>\n      <td>-0.246196</td>\n      <td>-1.293947</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>0.508724</td>\n      <td>0.491179</td>\n      <td>0.511179</td>\n      <td>-0.430321</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>-0.132347</td>\n      <td>-0.218552</td>\n      <td>-0.705951</td>\n      <td>1.288782</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 172,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.query('A > B')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-30T06:16:10.866582Z",
     "start_time": "2023-11-30T06:16:10.788796Z"
    }
   },
   "id": "ad617fafc67095ca"
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "outputs": [
    {
     "data": {
      "text/plain": "          A         B         C         D\n0 -0.468850 -0.575355 -1.755448 -0.721407\n5  0.424593  0.006147 -0.542266  0.060362\n6  0.326262 -0.133568 -0.246196 -1.293947\n9 -0.132347 -0.218552 -0.705951  1.288782",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>-0.468850</td>\n      <td>-0.575355</td>\n      <td>-1.755448</td>\n      <td>-0.721407</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>0.424593</td>\n      <td>0.006147</td>\n      <td>-0.542266</td>\n      <td>0.060362</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>0.326262</td>\n      <td>-0.133568</td>\n      <td>-0.246196</td>\n      <td>-1.293947</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>-0.132347</td>\n      <td>-0.218552</td>\n      <td>-0.705951</td>\n      <td>1.288782</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 173,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.query('A > B & B > C')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-30T06:16:10.867376Z",
     "start_time": "2023-11-30T06:16:10.794779Z"
    }
   },
   "id": "13e0699d5ed01479"
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "outputs": [
    {
     "data": {
      "text/plain": "          A         B         C         D\n0 -0.468850 -0.575355 -1.755448 -0.721407\n1  0.626830  0.142729  0.959875  0.695038\n2 -0.325520 -1.051279  0.077001 -0.976853\n3  0.620873  0.115614  0.203067 -1.259401\n4 -0.628626  0.230832 -1.168348  0.427251\n5  0.424593  0.006147 -0.542266  0.060362\n6  0.326262 -0.133568 -0.246196 -1.293947\n8  0.508724  0.491179  0.511179 -0.430321\n9 -0.132347 -0.218552 -0.705951  1.288782",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>-0.468850</td>\n      <td>-0.575355</td>\n      <td>-1.755448</td>\n      <td>-0.721407</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.626830</td>\n      <td>0.142729</td>\n      <td>0.959875</td>\n      <td>0.695038</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>-0.325520</td>\n      <td>-1.051279</td>\n      <td>0.077001</td>\n      <td>-0.976853</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0.620873</td>\n      <td>0.115614</td>\n      <td>0.203067</td>\n      <td>-1.259401</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>-0.628626</td>\n      <td>0.230832</td>\n      <td>-1.168348</td>\n      <td>0.427251</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>0.424593</td>\n      <td>0.006147</td>\n      <td>-0.542266</td>\n      <td>0.060362</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>0.326262</td>\n      <td>-0.133568</td>\n      <td>-0.246196</td>\n      <td>-1.293947</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>0.508724</td>\n      <td>0.491179</td>\n      <td>0.511179</td>\n      <td>-0.430321</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>-0.132347</td>\n      <td>-0.218552</td>\n      <td>-0.705951</td>\n      <td>1.288782</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 174,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.query('A > B | B > C')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-30T06:16:10.868485Z",
     "start_time": "2023-11-30T06:16:10.801262Z"
    }
   },
   "id": "5ba0b03e20b9353c"
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "outputs": [
    {
     "data": {
      "text/plain": "                  B         C         D\nA                                      \n-1.358186 -0.768316 -0.553000 -0.814419\n-0.468850 -0.575355 -1.755448 -0.721407\n-0.325520 -1.051279  0.077001 -0.976853\n-0.132347 -0.218552 -0.705951  1.288782\n 0.326262 -0.133568 -0.246196 -1.293947\n 0.424593  0.006147 -0.542266  0.060362\n 0.508724  0.491179  0.511179 -0.430321\n 0.620873  0.115614  0.203067 -1.259401\n 0.626830  0.142729  0.959875  0.695038",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n    </tr>\n    <tr>\n      <th>A</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>-1.358186</th>\n      <td>-0.768316</td>\n      <td>-0.553000</td>\n      <td>-0.814419</td>\n    </tr>\n    <tr>\n      <th>-0.468850</th>\n      <td>-0.575355</td>\n      <td>-1.755448</td>\n      <td>-0.721407</td>\n    </tr>\n    <tr>\n      <th>-0.325520</th>\n      <td>-1.051279</td>\n      <td>0.077001</td>\n      <td>-0.976853</td>\n    </tr>\n    <tr>\n      <th>-0.132347</th>\n      <td>-0.218552</td>\n      <td>-0.705951</td>\n      <td>1.288782</td>\n    </tr>\n    <tr>\n      <th>0.326262</th>\n      <td>-0.133568</td>\n      <td>-0.246196</td>\n      <td>-1.293947</td>\n    </tr>\n    <tr>\n      <th>0.424593</th>\n      <td>0.006147</td>\n      <td>-0.542266</td>\n      <td>0.060362</td>\n    </tr>\n    <tr>\n      <th>0.508724</th>\n      <td>0.491179</td>\n      <td>0.511179</td>\n      <td>-0.430321</td>\n    </tr>\n    <tr>\n      <th>0.620873</th>\n      <td>0.115614</td>\n      <td>0.203067</td>\n      <td>-1.259401</td>\n    </tr>\n    <tr>\n      <th>0.626830</th>\n      <td>0.142729</td>\n      <td>0.959875</td>\n      <td>0.695038</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 175,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.query('A > B & B > C | C > D').groupby('A').sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-30T06:16:10.880981Z",
     "start_time": "2023-11-30T06:16:10.811269Z"
    }
   },
   "id": "6a7bd15bc2b4090e"
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   a    b  c\n",
      "0  1  2.0  3\n",
      "1  1  NaN  4\n",
      "2  2  1.0  3\n",
      "3  1  2.0  2\n"
     ]
    },
    {
     "data": {
      "text/plain": "     a  c\nb        \n1.0  2  3\n2.0  2  5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>c</th>\n    </tr>\n    <tr>\n      <th>b</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1.0</th>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>2.0</th>\n      <td>2</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 176,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = [[1, 2, 3], [1, None, 4], [2, 1, 3], [1, 2, 2]]\n",
    "df = pd.DataFrame(l, columns=[\"a\", \"b\", \"c\"])\n",
    "print(df.head())\n",
    "df.groupby('b').sum()\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-30T06:16:10.881923Z",
     "start_time": "2023-11-30T06:16:10.814054Z"
    }
   },
   "id": "ff20a3994c8be66d"
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "outputs": [
    {
     "data": {
      "text/plain": "     a  c\nb        \n1.0  2  3\n2.0  2  5\nNaN  1  4",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>c</th>\n    </tr>\n    <tr>\n      <th>b</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1.0</th>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>2.0</th>\n      <td>2</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>NaN</th>\n      <td>1</td>\n      <td>4</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 177,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(by=[\"b\"], dropna=False).sum()\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-30T06:16:10.882116Z",
     "start_time": "2023-11-30T06:16:10.822268Z"
    }
   },
   "id": "6e34ec4702d2bd75"
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          col\n",
      "A a  1.142155\n",
      "  b -0.562651\n",
      "B a  0.082494\n",
      "  b  3.012826\n",
      "          0         1         2\n",
      "1 -0.184636  0.947877 -0.079786\n",
      "2 -1.282304 -0.824027  0.244820\n",
      "3  0.491465  0.629511 -0.447944\n"
     ]
    }
   ],
   "source": [
    "index = pd.MultiIndex.from_product([[\"A\", \"B\"], [\"a\", \"b\"]])\n",
    "df = pd.DataFrame(np.random.randn(4), index=index, columns=[\"col\"])\n",
    "print(df.head())\n",
    "\n",
    "idx = pd.Index([1, 2, 3])\n",
    "df = pd.DataFrame(np.random.randn(3, 3), index=idx)\n",
    "print(df.head())\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-30T06:16:10.882256Z",
     "start_time": "2023-11-30T06:16:10.828330Z"
    }
   },
   "id": "22effbd055017a7e"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# select"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1aa200b2145b858d"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## select columns"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "e730a9acfdaad1be"
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "outputs": [
    {
     "data": {
      "text/plain": "          A         B\n0  0.769845 -0.168352\n1 -1.961180  0.716396\n2 -0.816087 -1.468212\n3 -1.144079  0.490981\n4 -0.614567  0.007935",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.769845</td>\n      <td>-0.168352</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>-1.961180</td>\n      <td>0.716396</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>-0.816087</td>\n      <td>-1.468212</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>-1.144079</td>\n      <td>0.490981</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>-0.614567</td>\n      <td>0.007935</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 179,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'))\n",
    "selected_columns = df[['A', 'B']]\n",
    "selected_columns.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-30T06:16:10.882491Z",
     "start_time": "2023-11-30T06:16:10.833882Z"
    }
   },
   "id": "60e621c249cb460b"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## select rows"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ff8ad33cc798139f"
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "outputs": [
    {
     "data": {
      "text/plain": "          A         B         C         D\n2 -0.816087 -1.468212  1.133745 -0.656113\n3 -1.144079  0.490981 -0.007495 -0.221374\n4 -0.614567  0.007935 -0.438814  1.171975",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2</th>\n      <td>-0.816087</td>\n      <td>-1.468212</td>\n      <td>1.133745</td>\n      <td>-0.656113</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>-1.144079</td>\n      <td>0.490981</td>\n      <td>-0.007495</td>\n      <td>-0.221374</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>-0.614567</td>\n      <td>0.007935</td>\n      <td>-0.438814</td>\n      <td>1.171975</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 180,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "selected_rows = df.iloc[2:5]\n",
    "selected_rows.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-30T06:16:10.882648Z",
     "start_time": "2023-11-30T06:16:10.839353Z"
    }
   },
   "id": "9f83c029107c1103"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Conditional Selection"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a0fae1c1509ba38a"
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "outputs": [
    {
     "data": {
      "text/plain": "          A         B         C         D\n0  0.769845 -0.168352  0.635470 -0.020493\n6  1.286383 -1.099055 -0.122614  1.241066",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.769845</td>\n      <td>-0.168352</td>\n      <td>0.635470</td>\n      <td>-0.020493</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>1.286383</td>\n      <td>-1.099055</td>\n      <td>-0.122614</td>\n      <td>1.241066</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 181,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "selected_rows = df[df['A'] > 0.5]\n",
    "selected_rows.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-30T06:16:10.882802Z",
     "start_time": "2023-11-30T06:16:10.844518Z"
    }
   },
   "id": "da3f58e154af4cb2"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Where"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f51ae8cd7d2b4002"
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          A         B         C         D\n",
      "0 -1.111456 -0.738958  0.282170 -0.754432\n",
      "1  0.363839 -0.298873  0.080403  1.113625\n",
      "2  0.922855 -0.349329  1.476782  1.421746\n",
      "3 -0.475355 -0.729138  0.266818  0.430500\n",
      "4 -1.188233 -1.170551  2.790584 -0.483027\n"
     ]
    },
    {
     "data": {
      "text/plain": "          A   B         C         D\n0       NaN NaN  0.282170       NaN\n1  0.363839 NaN  0.080403  1.113625\n2  0.922855 NaN  1.476782  1.421746\n3       NaN NaN  0.266818  0.430500\n4       NaN NaN  2.790584       NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>0.282170</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.363839</td>\n      <td>NaN</td>\n      <td>0.080403</td>\n      <td>1.113625</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.922855</td>\n      <td>NaN</td>\n      <td>1.476782</td>\n      <td>1.421746</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>0.266818</td>\n      <td>0.430500</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>2.790584</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 182,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'))\n",
    "print(df.head())\n",
    "df = df.where(df > 0)\n",
    "df.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-30T06:16:10.883710Z",
     "start_time": "2023-11-30T06:16:10.854417Z"
    }
   },
   "id": "b3a97223dacf421b"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Group By"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1b1e40a9aa140a74"
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     A      B         C         D\n",
      "0  foo    one -1.761914  0.751698\n",
      "1  bar    one -0.479532  0.573398\n",
      "2  foo    two -1.360444  1.297496\n",
      "3  bar  three -0.866444  1.176017\n",
      "4  foo    two -0.936136 -1.097631\n"
     ]
    },
    {
     "data": {
      "text/plain": "                     B         C         D\nA                                         \nbar        onethreetwo -1.269738  1.358862\nfoo  onetwotwoonethree -5.280005  1.541418",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n    </tr>\n    <tr>\n      <th>A</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>bar</th>\n      <td>onethreetwo</td>\n      <td>-1.269738</td>\n      <td>1.358862</td>\n    </tr>\n    <tr>\n      <th>foo</th>\n      <td>onetwotwoonethree</td>\n      <td>-5.280005</td>\n      <td>1.541418</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 183,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar',\n",
    "                         'foo', 'bar', 'foo', 'foo'],\n",
    "                   'B': ['one', 'one', 'two', 'three',\n",
    "                         'two', 'two', 'one', 'three'],\n",
    "                   'C': np.random.randn(8),\n",
    "                   'D': np.random.randn(8)})\n",
    "print(df.head())\n",
    "df.groupby('A').sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-30T06:16:10.884039Z",
     "start_time": "2023-11-30T06:16:10.854669Z"
    }
   },
   "id": "1e0cb0b1213f8643"
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "e74c734f211af248"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 原始的 Python 列表\n",
    "list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
    "arr = np.array(list_of_lists)\n",
    "print(arr)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ae25fa6d04a5b9ea"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "4289ccaa9135e13e"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
